package mx.kolobok.noteeditor.ui.keymap.actions;

import mx.kolobok.noteeditor.NoteManagerException;
import mx.kolobok.noteeditor.ui.MessageDialogs;
import mx.kolobok.noteeditor.ui.list.NoteListModel;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * User:  Nikita Belenkiy
 * Date: 19.05.11
 * Time: 17:39
 */
public class DeleteNoteAction extends AbstractAction {
    private JList noteList;
    private NoteListModel noteListModel;

    public DeleteNoteAction(JList noteList, NoteListModel noteListModel) {
        super("Delete");
//        putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
        putValue(Action.ACCELERATOR_KEY, Actions.DELETE_KEYSTROKE);

        this.noteList = noteList;
        this.noteListModel = noteListModel;
    }

    public void actionPerformed(ActionEvent e) {
        deleteSelectedItem();
    }

    private void deleteSelectedItem() {
        int selectedIndex = noteList.getSelectedIndex();
        if (selectedIndex >= 0) {
            if (MessageDialogs.doYouReallyWantToDeleteThisNote(SwingUtilities.getWindowAncestor(noteList))) {
                try {
                    noteListModel.deleteNote(selectedIndex);
                } catch (NoteManagerException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
